const log4js = require("../utils/log4js");

const logger = async (ctx, next) => {
  try {
    const startTime = new Date().getTime();
    log4js.info(`request: ${ctx.method} ${ctx.url}  - ip: ${ctx.request.ip}`);
    log4js.info(`request body: ${JSON.stringify(ctx.request.body)}`);
    await next();
    log4js.info(`response - status: ${ctx.status} - res: ${ctx.body}`);
    log4js.info(`---end| costTime = ${new Date().getTime() - startTime}---`);
  } catch (error) {
    if (error.errorCode) {
      return (ctx.body = error.msg);
    }
    log4js.error(`---error系统报错信息---${error}`);
  }
};

module.exports = logger;
